//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModelPathwiseMultiCaplet.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Pathwise;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelPathwiseMultiProduct.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::CMarketModelPathwiseMultiCaplet (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ accruals, Cephei::Core::IVector<Double>^ paymentTimes, Cephei::Core::IVector<Double>^ strikes) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiCaplet::typeid)
{
    try
    {
#ifdef HANDLE
        _phMarketModelPathwiseMultiCaplet = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        accruals->Lock();
        INativeVector<Double>^ _NCIaccruals = accruals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCaccruals = safe_cast<CDoubleVector^>(_NCIaccruals);
        std::vector<QuantLib::Real>& _accruals = static_cast<std::vector<QuantLib::Real>&> (_NCaccruals->GetReference ());
        paymentTimes->Lock();
        INativeVector<Double>^ _NCIpaymentTimes = paymentTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCpaymentTimes = safe_cast<CDoubleVector^>(_NCIpaymentTimes);
        std::vector<QuantLib::Time>& _paymentTimes = static_cast<std::vector<QuantLib::Time>&> (_NCpaymentTimes->GetReference ());
        strikes->Lock();
        INativeVector<Double>^ _NCIstrikes = strikes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCstrikes = safe_cast<CDoubleVector^>(_NCIstrikes);
        std::vector<QuantLib::Rate>& _strikes = static_cast<std::vector<QuantLib::Rate>&> (_NCstrikes->GetReference ());
        _ppMarketModelPathwiseMultiCaplet = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiCaplet> (new QuantLib::MarketModelPathwiseMultiCaplet ( _rateTimes,  _accruals,  _paymentTimes,  _strikes ));
        SetMarketModelPathwiseMultiProduct (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiCaplet));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
        if (accruals != nullptr) accruals->Unlock();    //not optional
        if (paymentTimes != nullptr) paymentTimes->Unlock();    //not optional
        if (strikes != nullptr) strikes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::CMarketModelPathwiseMultiCaplet (boost::shared_ptr<QuantLib::MarketModelPathwiseMultiCaplet>& childNative, Object^ owner) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiCaplet = NULL;
#endif
	_ppMarketModelPathwiseMultiCaplet = &childNative;
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiCaplet));
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::CMarketModelPathwiseMultiCaplet (QuantLib::MarketModelPathwiseMultiCaplet& childNative, Object^ owner) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiCaplet = NULL;
#endif
	_ppMarketModelPathwiseMultiCaplet = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiCaplet> (&childNative);
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiCaplet));
    _MarketModelPathwiseMultiCapletOwner = owner;
    _MarketModelPathwiseMultiProductOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::CMarketModelPathwiseMultiCaplet (CMarketModelPathwiseMultiCaplet^ copy) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiCaplet = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelPathwiseMultiCaplet = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiCaplet> (copy->GetShared());
        _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiCaplet));
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::CMarketModelPathwiseMultiCaplet (PLATFORM::Type^ t) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiCaplet = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelPathwiseMultiCaplet::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::CMarketModelPathwiseMultiCaplet (QuantLib::Handle<QuantLib::MarketModelPathwiseMultiCaplet>& childNative, Object^ owner)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiCaplet::typeid)
{
	_phMarketModelPathwiseMultiCaplet = &childNative;
	_ppMarketModelPathwiseMultiCaplet = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseMultiCaplet>>(childNative.currentLink());
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiCaplet));
    _MarketModelPathwiseMultiCapletOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::CMarketModelPathwiseMultiCaplet (QuantLib::Handle<QuantLib::MarketModelPathwiseMultiCaplet> childNative)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiCaplet::typeid)
{
	_phMarketModelPathwiseMultiCaplet = &childNative;
	_ppMarketModelPathwiseMultiCaplet = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseMultiCaplet>>(childNative.currentLink());
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiCaplet));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::CMarketModelPathwiseMultiCaplet (QuantLib::MarketModelPathwiseMultiCaplet childNative)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseMultiCaplet::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseMultiCaplet = NULL;
#endif
	_ppMarketModelPathwiseMultiCaplet = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiCaplet> (new QuantLib::MarketModelPathwiseMultiCaplet (childNative));
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseMultiCaplet));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::~CMarketModelPathwiseMultiCaplet ()
{
    if (_ppMarketModelPathwiseMultiCaplet != NULL)
    {
	    delete _ppMarketModelPathwiseMultiCaplet;
        _ppMarketModelPathwiseMultiCaplet = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::!CMarketModelPathwiseMultiCaplet ()
{
    if (_ppMarketModelPathwiseMultiCaplet != NULL)
    {
	    delete _ppMarketModelPathwiseMultiCaplet;
    }
}
QuantLib::MarketModelPathwiseMultiCaplet& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::GetReference ()
{
    if (_ppMarketModelPathwiseMultiCaplet == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModelPathwiseMultiCaplet;
}
boost::shared_ptr<QuantLib::MarketModelPathwiseMultiCaplet>& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::GetShared ()
{
    if (_ppMarketModelPathwiseMultiCaplet == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModelPathwiseMultiCaplet;
}
QuantLib::MarketModelPathwiseMultiCaplet* Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::GetPointer ()
{
    if (_ppMarketModelPathwiseMultiCaplet == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModelPathwiseMultiCaplet;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelPathwiseMultiCaplet>& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::GetHandle ()
{
	if (_phMarketModelPathwiseMultiCaplet == NULL)
	{
		_phMarketModelPathwiseMultiCaplet = new Handle<QuantLib::MarketModelPathwiseMultiCaplet> (*_ppMarketModelPathwiseMultiCaplet);
	}
	return *_phMarketModelPathwiseMultiCaplet;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::HasNative () 
{
	return (_ppMarketModelPathwiseMultiCaplet != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::AlreadyDeflated::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppMarketModelPathwiseMultiCaplet)->alreadyDeflated ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppMarketModelPathwiseMultiCaplet)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelPathwiseMultiCaplet)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelPathwiseMultiCaplet)->numberOfProducts ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMarketModelPathwiseMultiCaplet)->possibleCashFlowTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::IMarketModelPathwiseMultiCaplet^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMarketModelPathwiseMultiCaplet)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet::SuggestedNumeraires::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppMarketModelPathwiseMultiCaplet)->suggestedNumeraires ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Products::Pathwise::IMarketModelPathwiseMultiCaplet^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseMultiCaplet_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ accruals, Cephei::Core::IVector<Double>^ paymentTimes, Cephei::Core::IVector<Double>^ strikes)
{
    return REFNEW CMarketModelPathwiseMultiCaplet ( rateTimes,  accruals,  paymentTimes,  strikes);
}
